public class Solution {
    public int uniquePaths(int m, int n) {
        //dp[i][j] 表示到(i,j)位置的方法数
        //dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        //dp[0][j] = 1, dp[i][0] = 1
        //从左向右
        //reutrn dp[m - 1][n - 1]
        int[][] dp = new int[m][n];
        for(int i = 0; i < n; i++){
            dp[0][i] = 1;
        }
        for(int i = 0; i < m; i++){
            dp[i][0] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];

    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        int flag = 0;
        for(int i = 0; i < n; i++){
            if(obstacleGrid[0][i] == 1){
                flag = 1;
            }
            if(flag == 1){
                dp[0][i] = 0;
            }else{
                dp[0][i] = 1;
            }
        }
        flag = 0;
        for(int i = 0; i < m; i++){
            if(obstacleGrid[i][0] == 1){
                flag = 1;
            }
            if(flag == 1){
                dp[i][0] = 0;
            }else{
                dp[i][0] = 1;
            }
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++) {
                if(obstacleGrid[i][j] == 1){
                    dp[i][j] = 0;
                }else{
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }

    public int maxValue(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = grid[0][0];
        for(int i = 1; i < n; i++){
            dp[0][i] = dp[0][i - 1] + grid[0][i];
        }
        for(int i = 1; i < m; i++){
            dp[i][0] = dp[i - 1][0] + grid[i][0];
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
            }
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                System.out.print(dp[i][j] + " ");
            }
            System.out.println();
        }
        return dp[m - 1][n - 1];
    }

    public int minFallingPathSum(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dp = new int[m][n + 2];
        for(int i = 0; i < m; i++ ){
            dp[i][0] = Integer.MAX_VALUE;
            dp[i][n + 1] = Integer.MAX_VALUE;
        }
        for(int i = 1; i <= n; i++){
            dp[0][i] = matrix[0][i - 1];
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j + 1])) + matrix[i][j];
            }
        }
        int ret = Integer.MAX_VALUE;
        for(int i = 1; i <= n; i++){
            ret = Math.min(ret, dp[m - 1][i]);
        }
        return ret;
    }
}
